angular.module('app')

.controller('CatListController', function($scope,StockService,StockCategoryService) {
	
	$scope.paginationConf_stocks = {
            currentPage: 1,
            totalItems: 0,
            itemsPerPage: 30,
            pagesLength: 8,
            perPageOptions: [10, 20, 30, 40, 50],
            rememberPerPage: 'perPageItems',
            onChange: function(){
				
            }
        };
	$scope.paginationConf_categories = {
            currentPage: 1,
            totalItems: 0,
            itemsPerPage: 30,
            pagesLength: 8,
            perPageOptions: [10, 20, 30, 40, 50],
            rememberPerPage: 'perPageItems',
            onChange: function(){
				
            }
        };
	// init categories
	StockCategoryService.findAllStockCategoies().success( 
		function(data) {
			$scope.categories = data ? data : [];
			$scope.paginationConf_categories.totalItems=$scope.categories.length;
			if ($scope.categories.length != 0) {
				$scope.selectedCategory = $scope.categories[0];
				$scope.selectedIndex = 0;
				StockService.findStocksByCategory($scope.selectedCategory.id).success(
					function(data) {
						$scope.stocksByCategory = data;
						//$scope.selectedStock = data[0];
						$scope.paginationConf_stocks.totalItems=$scope.stocksByCategory.length;
					}
				);
			}
		}
    );
	
	$scope.createStock=function() {
        $scope.stocksByCategory.splice(0,0,{code:'', name:'', status:''});
        $scope.paginationConf_stocks.totalItems = $scope.paginationConf_stocks.totalItems + 1;
    };
	
    $scope.createStockCategory=function() {
		console.log("start calling createStockCategory");
        $scope.categories.splice(0,0,{name:''});
        $scope.paginationConf_categories.totalItems = $scope.paginationConf_categories.totalItems + 1;
    };
	
	$scope.selectCurrentStockCategory=function(index) {
		$scope.selectedCategory = $scope.categories[index];
		$scope.selectedIndex = index;

		if ($scope.selectedCategory.id) {
			StockService.findStocksByCategory($scope.selectedCategory.id).success( 
				function(data) {
					$scope.stocksByCategory = data;
					$scope.paginationConf_stocks.totalItems=$scope.stocksByCategory.length;
				}
			);
		} else {
			// new stock category
			$scope.stocksByCategory = [];
			$scope.paginationConf_stocks.totalItems=0;
		}
		
		console.log($scope.rootVariable);
		
    };
    
	$scope.deleteStockCategory=function(stockCategoryId) {
		StockCategoryService.deleteStockCategory(stockCategoryId).success( 
			function(data) {
				for(var i=0; i<$scope.categories.length; i++){
					if($scope.categories[i].id==stockCategoryId){
						$scope.categories.splice(i,1);
						if ($scope.categories.length > 0) {
							$scope.selectCurrentStockCategory(0);
						} else {
							$scope.selectedCategory = {};
							$scope.stocksByCategory = [];
							$scope.paginationConf_stocks.totalItems=0;
						}
					}
				}
			}
		).error(
			function(data, status, headers, config) {
				$('#warningMsgForCat').html("<font color='red'>Error in deleting selected cateogory!</font>");
			}
		);
	}
	
	$scope.deleteStockCatRelate=function(stockId) {
		StockCategoryService.deleteStockCatRelate($scope.selectedCategory.id, stockId).success( 
			function(data) {
				for(var i=0; i<$scope.stocksByCategory.length; i++){
					if($scope.stocksByCategory[i].id==stockId){
						$scope.stocksByCategory.splice(i,1);
						$scope.paginationConf_stocks.totalItems=Math.max($scope.paginationConf_stocks.totalItems-1, 0);
					}
				}
			}
		).error(
			function(data, status, headers, config) {
				$('#warningMsgForCat').html("<font color='red'>Error in deleting selected cateogory!</font>");
			}
		);
	}
	
	
	//////////////////Initialization /////////////////////

	$scope.currentPage = "CategoryMgr";
	
    // used for e/u/d stock
    $scope.showStockEdit = true;
	$scope.showCatEdit = true;
    $scope.master = {};
	
	$scope.stocksByCategory = [];
	
	//////////////////Initialization /////////////////////

})